<?php

namespace Home\Controller;

class MatchV4Controller extends HomeController
{
    public function _initialize()
    {
        parent::_initialize();

        C('DEFAULT_THEME', 'v4');
        $this->currentTab = 'match';
    }

    public function index()
    {
        $mEvent = event('match');
        $matches = $mEvent->lists();

        foreach ($matches as $key => &$value ) {
            uri_format($value['logo']);
            uri_format($value['pc_cover']);
            uri_format($value['cover']);
        }
        $this->matches = $matches;

        $this->hot_list = $this->_hot();
        $this->forcast_list = $this->_forcast();
        $this->show_id = ['group_id' => $this->hot_list[0]['list'][0]['id']];
        $this->newslist = event('news')->lists(['match_id' => ['$gt' => 0]], 1, 5);

        $this->title = "赛事";
        setRedirect();
        $this->display();
    }

    /**
     * 热门比赛
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    private function _hot(){
        $rgEvent = event('RaceGroup');
        $curDate = date('Y-m-d H:i', time());
        $map = [
            'state' => 1,
            'race_time' => ['lt', $curDate]
        ];
        $ids = $rgEvent->lists($map, 'id', C('VIDOE_LIST_ROWS'), 0, 'race_time desc');

        return $this->_format_races($ids);
    }

    /**
     * 预告比赛
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    private function _forcast(){
        $rgEvent = event('RaceGroup');
        $curDate = date('Y-m-d H:i', time());
        $map = [
            'race_time' => ['$gt' => $curDate]
        ];

        $ids = $rgEvent->lists($map, 'id', C('VIDOE_LIST_ROWS'), 0, 'race_time asc');

        $fieldMap = ['id', 'title', 'subtitle', 'race_date', 
                    'race_hour', 'team', 'is_scribe', 'game', 'match', 'cover'];
        $items = [];
        foreach ($ids as $key => $value) {
            $detail = $rgEvent->info($value['id']);
            $detail['game'] = $this->field_filter($detail['game'], array('id','name','logo'));
            $detail = $this->field_filter($detail, $fieldMap);
            uri_format($detail['cover']);
            uri_format($detail['match']['cover']);
            uri_format($detail['match']['pc_cover']);
            if (empty($detail['match']['pc_cover'])) {
                $detail['match']['pc_cover'] = $detail['match']['cover'];
            }
            $items[$detail['race_date']][] = $detail;
        }

        foreach ($items as $key => $value) {
            $data[] = array('date'=>$key, 'list'=>$value);
        }
        return $data;
    }

    public function all()
    {

        $mEvent = event('match');
        $matches = $mEvent->lists(1, 100);

        foreach ($matches as $key => &$value ) {
            uri_format($value['logo']);
            uri_format($value['pc_cover']);
            uri_format($value['cover']);
        }

        $this->matches = array_values($matches);
        $this->matches_16 = array_slice($this->matches, 0, 16);

        $this->title = "全部赛事";
        setRedirect();
        $this->display();
    }

    /**
     * 赛事游戏分类
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function game($id = 0)
    {
        $id = intval($id);
        if (empty($id)) {
            return $this->redirect('/match-all');
        }
        $game = event('game')->info($id);
        if (empty($game)) {
            return $this->redirect('/match-all');
        }
        uri_format($game['logo']);
        $this->game = $game;

        $race = event('RaceGroup')->lists(['game_id' => $id, 'state' => 1], 'id,match_id', 1, 1, 'race_time desc');
        $race = array_values($race);
        $this->show_id = ['group_id' => $race[0]['id']];

        $mEvent = event('match');
        $matches = $mEvent->lists(1, 100, false, '', $id);
        foreach ($matches as $key => &$value ) {
            uri_format($value['logo']);
            uri_format($value['pc_cover']);
            uri_format($value['pc_bg_detail']);
            uri_format($value['pc_bg_player']);
            uri_format($value['cover']);
            $value['game'] = $game;

            if ($value['id'] == $race[0]['match_id']) {
                $this->bg_img = $value['pc_bg_player'];
            }
        }
        $this->matches = array_values($matches);


        $this->title = "{$game['name']}赛事";
        setRedirect();
        $this->display();
    }

    public function races($id = 0)
    {
        $id = intval($id);
        if (empty($id)) {
            return $this->redirect('/match');
        }
        $mEvent = event('match');
        $vEvent = event('video');

        $match = $mEvent->info($id);
        if (empty($match)) {
            return $this->redirect('/match');
        }

        uri_format($match['cover']);
        uri_format($match['pc_cover']);
        uri_format($match['pc_bg_player']);
        uri_format($match['pc_bg_detail']);
        $this->match = $match;

        $game = event('game')->info($match['game_id']);
        uri_format($game['logo']);
        $this->game = $game;

        $rgEvent = event('RaceGroup');
        $raceDates = $rgEvent->raceTimeGroup($id, 1, 100);
        if($raceDates) {
            $map = [
                'match_id' => $id,
                'state' => 1,
                'race_date' => ['in', $raceDates]
            ];
            $ids = $rgEvent->lists($map, 'id', 0, 0, 'race_time desc');
        }

        $this->race_list = $this->_format_races($ids);

        $this->show_id = ['group_id' => $this->race_list[0]['list'][0]['id']];
        $this->newslist = event('news')->lists(['match_id' => $id], 1, 5);

        $this->title = $match['title'];
        setRedirect();
        $this->display();
    }

    public function group($id = 0)
    {
        $id = intval($id);
        if (!$id) {
            $this->error('比赛信息有误');
        }
        $vService = service('video');
        $map = array(
                'group_id'=>$id,
                'status'=>$vService::STATUS_PASS,
                'type'=>$vService::TYPE_MATCH
            );
        $races = $vService->where($map)->order('round asc')->getField('id', true);
        $race_id = intval($races[0]);
        if (!$race_id) {
            $this->error('比赛信息有误');
        }
        $this->redirect('/match-detail-'.$race_id);
    }

    public function detail($id = 0)
    {
        if (IS_MOBILE) {
            $this->redirect(U('share/match', ['id' => $id]));
        }
        if (!$id) {
            $this->error('比赛信息有误');
        }
        $vEvent = event('video');
        $video = $vEvent->detail($id, UID);
        if ( !$video || !isset($video['match_videos'])) {
            $this->redirect('Index/index_redirect');
        }
        if ($video['type'] == 0) {
            $redirect_url = U('Home/share/video', array('id' => $id));
            $this->redirect($redirect_url);
        }

        $match = event('match')->info($video['match_id']);
        uri_format($match['cover']);
        uri_format($match['pc_cover']);
        uri_format($match['pc_bg_player']);
        uri_format($match['pc_bg_detail']);
        $this->match = $match;

        A('Api/History', 'Event')->add(UID, $id);

        $rgEvent = event('RaceGroup');

        $map['match_id'] = $video['match_id'];
        $map = [
            'match_id' => $video['match_id'],
            'state' => 1,
            'id' => ['$ne' => $video['group_id']],
        ];
        $raceIds = $rgEvent->lists($map, 'id', 5, 1, 'race_time desc');
        $this->race_list = $this->_format_races($raceIds, false);

        setRedirect();
        $this->show_id = ['race_id' => $id];
        $this->title = $video['detail'];
        $this->display('detail');
    }

    public function news_list($id = 0)
    {
        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $id = intval($id);

        $nEvent = event('news');
        $map['match_id'] = $id?:['$gt' => 0];

        $newslist = $nEvent->lists($map, $p, $limit);

        if (IS_AJAX) {
            $this->ajaxReturn(['state' => 0, 'info' => '操作成功', 'data' => $newslist]);
        }
        if ($id) {
            $match = event('match')->info($id);
            uri_format($match['cover']);
            uri_format($match['pc_cover']);
            uri_format($match['pc_bg_player']);
            uri_format($match['pc_bg_detail']);
            $this->match = $match;
        }

        $rgEvent = event('RaceGroup');

        $raceIds = $rgEvent->lists($map, 'id', 5, 1, 'create_time desc');
        $this->race_list = $this->_format_races($raceIds, false);

        $url = '/match-news';
        $id && $url .= '-'.$id;
        $this->url = $url;
        $this->pager = $limit;

        $this->news_count = $nEvent->count($map);
        $this->newslist = $newslist;
        $this->title = '赛事新闻';
        $this->display();
    }

    public function news_detail($id = 0)
    {
        $id = intval($id);
        if (!$id) {
            $this->error('参数有误');
        }

        $this->news = $news = event('news')->detail($id, false);

        if (!empty($news['match'])) {
            $map = ['match_id' => $news['match']['id']];
            $match = event('match')->info($news['match']['id']);
            uri_format($match['cover']);
            uri_format($match['pc_cover']);
            uri_format($match['pc_bg_player']);
            uri_format($match['pc_bg_detail']);
            $this->match = $match;
        } else {
            $map = ['match_id' => ['$gt' => 0]];
        }
        $raceIds = event('RaceGroup')->lists($map, 'id', 5, 1, 'create_time desc');
        $this->race_list = $this->_format_races($raceIds, false);

        $this->title = '新闻详情';
        $this->display();
    }

    private function _format_races($ids = [], $groupByDate = true) {
        $data = [];
        if(empty($ids)) return $data;
        $fieldMap = ['id', 'title', 'subtitle', 'race_date', 'race_hour', 'score', 'team', 'game', 'match', 'cover'];
        $rgEvent = event('RaceGroup');
        $vEvent = event('Video');
        foreach ($ids as $key => $value) {
            $detail = $rgEvent->info($value['id']);
            $detail['score'] = $detail['score']?array_map('intval', explode('-', $detail['score'])):[];
            $detail['game'] = $this->field_filter($detail['game'], ['id','name','logo']);
            $detail['match'] = $this->field_filter($detail['match'], ['id','title','cover', 'pc_cover']);
            $races = $vEvent->lists(['group_id' => $detail['id'], 'type' => 1], 0, true);
            if (!empty($races) && is_array($races)) {
                sort($races);
                $races = array_slice($races, 0, 4);
            }
            $detail = $this->field_filter($detail, $fieldMap);
            $detail['races'] = $races;
            $race[$detail['race_date']][] = $detail;
        }

        foreach ($race as $key => $value) {
            foreach ($value as $k => &$v) {
                uri_format($v['cover']);
                uri_format($v['match']['cover']);
                uri_format($v['match']['pc_cover']);
                if (empty($v['match']['pc_cover'])) {
                    $v['match']['pc_cover'] = $v['match']['cover'];
                }
            }
            if ($groupByDate) {
                $data[] = ['date'=>$key, 'list'=>$value];
            } else {
                $data = array_merge($data, $value);
            }
        }
        return $data;
    }
}